<?php


namespace app\app\controller;


use app\app\model\ProjectMeritEstimate;
use app\app\model\ProjectMeritFileDeclare;
use app\app\model\ProjectMeritModel;
use app\BaseController;

class ProjectMerit extends BaseController
{
    /**
     * 获取创优目标下拉框类列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMeritNameList()
    {
        $project_id = $this->request->param('project_id');
        $list = ProjectMeritModel::field('id,merit_name')
            ->where('project_id', $project_id)
            ->select();
        $this->success('success', $list);
    }

    /**
     * 获取创优目标评价列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMeritEstimate()
    {
        $merit_id = $this->request->param('merit_id');
        $list = ProjectMeritEstimate::where('merit_id', $merit_id)
            ->field('id,estimate_name,score')
            ->select();
        $this->success('success', $list);
    }

    /**
     * 获取专项评价完成情况完成/未完成比
     */
    public function getMeritEstimateNum()
    {
        $merit_id = $this->request->param('merit_id');
        $data['total'] = ProjectMeritEstimate::where('merit_id', $merit_id)
            ->count();
        $data['not_total'] = ProjectMeritEstimate::where('merit_id', $merit_id)
            ->where('score', 0)
            ->count();
        if ($data['not_total'] < $data['total']) {
            $data['is_true'] = 1; // 未完成
        } else {
            $data['is_true'] = 2; // 完成
        }
        $this->success('success', $data);
    }

    /**
     * 获取合规性文件清单|获取申报条件
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMeritFileList()
    {
        $merit_id = $this->request->param('merit_id');
        $type = $this->request->param('type');
        $list = ProjectMeritFileDeclare::where('merit_id', $merit_id)
            ->where('type', $type)
            ->field('id,name,is_read')
            ->select();
        $this->success('success', $list);
    }


    /**
     * 获取申报条件列表|合规性文件清单 完成/未完成比
     */
    public function getMeritFileDeclareNum()
    {
        $merit_id = $this->request->param('merit_id');
        $type = $this->request->param('type');
        $data['total'] = ProjectMeritFileDeclare::where('merit_id', $merit_id)
            ->where('type', $type)
            ->count();
        $data['not_total'] = ProjectMeritFileDeclare::where('merit_id', $merit_id)
            ->where('type', $type)
            ->where('is_read', 1) //1为未读，2为已读
            ->count();
        if ($data['not_total'] < $data['total']) {
            $data['is_true'] = 1; // 未完成
        } else {
            $data['is_true'] = 2; // 完成
        }
        $this->success('success', $data);
    }

    /**
     * 获取申报条件详情/合规性文件清单完成
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMeritFileDeclareInfo()
    {
        $id = $this->request->param('declare_id');
        $info = ProjectMeritFileDeclare::where('id', $id)
            ->find();
        $this->success('success', $info);
    }

    /**
     * 标记已读申报条件详情/合规性文件清单完成
     * @throws \think\db\exception\DbException
     */
    public function setMeritFileDeclare()
    {
        $id = $this->request->param('declare_id');
        if (!empty($id)) {
            $id = explode(',', $id);
        }
        $res = ProjectMeritFileDeclare::where('id', 'in', $id)
            ->update([
                'is_read' => 2
            ]);
        if (!$res) {
            $this->error('error', $res);
        }
        $this->success('success', $res);
    }

    /**
     * 创优目标评价评分
     * @throws \think\db\exception\DbException
     */
    public function setMeritEstimate()
    {
        $estimate_id = $this->request->param('estimate_id');
        $estimate_time = $this->request->param('estimate_time');
        $score = $this->request->param('score');
        $res = ProjectMeritEstimate::where('id', $estimate_id)
            ->update([
                'estimate_time' => $estimate_time,
                'score' => $score
            ]);
        if (!$res) {
            $this->error('error');
        }
        $this->success('success', $res);
    }
}